﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace NENAData
{
    public static class Extensions
    {

        public static T IsNull<T>(object value, T nullAlterative)
        {
            if (value != DBNull.Value)
            {
                Type type = typeof(T);
                if (type.IsGenericType &&
                    type.GetGenericTypeDefinition() == typeof(Nullable<>).GetGenericTypeDefinition())
                {
                    type = Nullable.GetUnderlyingType(type);
                }

                return (T)(type.IsEnum ? Enum.ToObject(type, Convert.ToInt32(value)) :
                    Convert.ChangeType(value, type));
            }
            else
                return nullAlterative;
        }

    }
}
